package com.lyz.dataStructure.dome;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 15:17
 * @Description:
 **/

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/7/13 15:17
 *@Description:
 **/
public class Transform {

    public static void main(String[] args) {
        String s = " -52.4511 ";

        System.out.println(transf(s));
    }

    public static double transf(String str){
        if(str==null||"".equals(str.trim())){
            return 0;
        }
        str =str.trim();
        //判断正负
        boolean isZF=false;
        if(str.startsWith("-")){
            isZF =true;
            //去负号
            str = str.substring(1);
        }
        //给一个完整值变量
        double result = 0;
        //将字符串分割成两部分
        String[] strNum = str.split("\\.");
        int intNum = 0;
        for(int i=0;i<strNum[0].length();i++){
            int val = strNum[0].charAt(i)-'0';
            if(val<0||val>9){
                intNum/=10;
                continue;
            }
            intNum +=val*mkInteger(strNum[0].length()-i-1);
        }
        //记录小数
        int decNumResult = 0;
        //记录不合法的数

        if(strNum.length>1){
            int num =0;
            for(int i=0;i<strNum[1].length();i++){
                int val = strNum[1].charAt(i)-'0';
                if(val<0||val>9){
                    decNumResult/=10;
                    num++;
                    continue;
                }
                decNumResult +=val*mkInteger(strNum[1].length()-i-1);
            }
            result =decNumResult*mkInteger1(strNum[1].length()-num);
        }

        result = result+intNum;
        if (isZF) {
            result *= -1;
        }
    return result;
    }

    public static int mkInteger(int n){
        int i =1;
        for(int j =0;j<n;j++){
            i*=10;
        }
        return i;
    }

    public static double mkInteger1(int n){
        double i =1;
        for(int j =0;j<n;j++){
            i/=10;
        }
        return i;
    }
}
